<?php

namespace App\V3\Api\Services;

use App\V3\Api\Models\StorageModel;
use App\V3\Api\Models\StorageManageModel;
use Illuminate\Support\Facades\DB;
use App\V3\Api\Models\StorageBatchModel;
use App\V3\Api\Models\SellPackageDetailModel;
use App\V3\Api\Models\SellPackageContentModel;
use App\V3\Api\Models\CheckStorageManageModel;
use App\V3\Api\Models\SerialNumberModel;
class CheckStorageManageService
{

    //仓库列表
    static public function getStorageList($mainId,$data)
    {
        return StorageModel::select('id','storage_name')
            ->where('main_id', $mainId)
            ->where('delete_flag', 0)
            ->orderBy('id', 'DESC')->get()->toArray();
           /*  ->paginate($data['pageSize'],['*'], '',$data['currentPage']); */
    }

    static public function getStorageGoodsList($storageId,$data){

        $result = StorageManageModel::select(
            'a.id',
            'type_id',
            'b.materiel_name',
            'b.materiel_img',
            'c.shelf_name',
            'd.location_name',
            'a.type_name',
            'a.materiel_no',
            DB::raw('SUM(in_storage_num) as total_amount')
            )
            ->from('storage_manage as a')
            ->leftJoin('materiel_type as b', 'b.id', '=', 'a.type_id')
            ->leftJoin('storage_shelf as c', 'c.id', '=', 'a.shelf_id')
            ->leftJoin('storage_shelf_location as d', 'd.id', '=', 'a.location_id')
            ->where('a.main_id', $data['main_id'])
            ->where('a.storage_id',$storageId)
            ->where('a.delete_flag',0)
            ->where('b.delete_flag',0)
            ->groupBy('type_id')
            ->groupBy('location_id')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);

            return $result;
    }

 //识别条码
    static public function readBatchNo($data)
    {
        $subBatchInfo = substr($data['batch_no'], -1, 1);

        if($subBatchInfo == 'a') {
            $result = SELF::readSellPackage($data['batch_no']);

            if(empty($result)) {
                return ['code' => 1, 'msg' => '暂无条码信息', 'data' => ''];
            }
            //$result['type'] = 1;
        }
        else {
            if(strpos($data['batch_no'], 'smyc')!==false){

                $result = SELF::readMateriel($data['batch_no']);
                if(empty($result)) {
                    return ['code' => 1, 'msg' => '暂无条码信息', 'data' => ''];
                }
            }else{
                $result = SELF::readSerialNumber($data['batch_no']);
                if(empty($result)) {
                    return ['code' => 1, 'msg' => '暂无条码信息', 'data' => ''];
                }
            }
            //$result['type'] = 0;
        }

        return ['code' => 0, 'msg' => 'success', 'data' => $result];
    }
    /**
     * 序列号查询（打码）
     * @param unknown $serialNubmer
     */
    static private function readSerialNumber($serialNubmer){


        return SerialNumberModel::select(
            'b.id',
            'b.type_id',
            'c.materiel_name',
            'c.materiel_img',
            'd.shelf_name',
            'e.location_name',
            'c.type_name',
            'c.materiel_no',
            DB::raw('SUM(in_storage_num) as total_amount')
            )
            ->from('serial_number as a')
            ->leftJoin('storage_manage as b', 'b.id', '=', 'a.storage_manage_id')
            ->leftJoin('materiel_type as c', 'b.type_id', '=', 'c.id')
            ->leftJoin('storage_shelf as d', 'd.id', '=', 'b.shelf_id')
            ->leftJoin('storage_shelf_location as e', 'e.id', '=', 'b.location_id')
            ->where('serial_no', $serialNubmer)
            ->first();
    }

    static private function readSellPackage($batchNo)
    {
        $packageInfo = SellPackageDetailModel::select('a.id')
            ->from('sell_package_detail as a')
            ->leftJoin('package as b', 'b.id', '=', 'a.package_id')
            ->where('package_no', $batchNo)
            ->first()->toArray();

         return  SellPackageContentModel::select(
                'b.id',
                'b.type_id',
                'a.materiel_name',
                'a.materiel_img',
                'd.shelf_name',
                'e.location_name',
                 DB::raw('SUM(in_storage_num) as total_amount')
                )->from('sell_package_content')
                ->leftJoin('materiel_type as a', 'a.id', '=', 'sell_package_content.materiel_type_id')
                ->leftJoin('storage_manage as b', 'b.id', '=', 'sell_package_content.storage_manage_id')
                ->leftJoin('storage_shelf as d', 'd.id', '=', 'b.shelf_id')
                ->leftJoin('storage_shelf_location as e', 'e.id', '=', 'b.location_id')
                ->where('package_detail_id',$packageInfo['id'])
                ->first();
    }



    //商品批次信息
    static private function readMateriel($batchNo)
    {
        return StorageBatchModel::select(
            'b.id',
            'b.type_id',
            'c.materiel_name',
            'c.materiel_img',
            'd.shelf_name',
            'e.location_name',
            'c.type_name',
            'c.materiel_no',
             DB::raw('SUM(in_storage_num) as total_amount')
        )
            ->from('storage_batch as a')
            ->leftJoin('storage_manage as b', 'b.id', '=', 'a.storage_manage_id')
            ->leftJoin('materiel_type as c', 'b.type_id', '=', 'c.id')
            ->leftJoin('storage_shelf as d', 'd.id', '=', 'b.shelf_id')
            ->leftJoin('storage_shelf_location as e', 'e.id', '=', 'b.location_id')
            ->where('batch_no', $batchNo)
            ->first();
    }

    static public function checkList($condition,$data,$keyword){
       return CheckStorageManageModel::select(
           'a.id','check_no','b.nickname','sg_type','check_time'
           )
        ->from('check_storage_manage as a')
        ->leftJoin('admin as b','b.id','=','a.user_id')
        ->where($condition)
        ->where(function($query) use ($keyword) {
            $query->orWhere('b.nickname', 'like', '%'.$keyword.'%');
            $query->orWhere('a.check_no', 'like', '%'.$keyword.'%');
        })
        ->groupBy('check_no')
        ->orderBy('check_time','desc')
        ->paginate($data['pageSize'],['*'], '',$data['currentPage']);

    }

    static public function checkSkGoodsList($condition,$data,$keyword){
             $query=CheckStorageManageModel::select(
                'a.type_id',
                'a.yk_flag',
                 'is_cover',
                 'c.materiel_name',
                'c.materiel_img',
                'a.check_num','a.yk_num',
                'b.in_storage_num',
                'd.shelf_name','e.location_name',
                 'b.storage_name','c.type_name',
                 'c.materiel_no','a.storage_manage_id'
                )
                ->from('check_storage_manage as a')
                ->leftJoin('storage_manage as b','b.id','=','a.storage_manage_id')
                ->leftJoin('materiel_type as c','c.id','=','a.type_id')
                ->leftJoin('storage_shelf as d', 'd.id', '=', 'b.shelf_id')
                ->leftJoin('storage_shelf_location as e', 'e.id', '=', 'b.location_id')
                ->where($condition)
                ->where('check_no',$data['check_no']);
                if(!empty($data['yk_flag'])){
                    $query->where('a.yk_flag',$data['yk_flag']);
                }
                if(!empty($keyword)){
                 $query->where(function($query) use ($keyword) {
                 $query->orWhere('c.materiel_name', 'like', '%'.$keyword.'%');
                 });
                }
                return $query->groupBy('storage_manage_id')
                ->groupBy('type_id')
                ->orderBy('check_time','desc')
                ->paginate($data['pageSize'],['*'], '',$data['currentPage'])->toArray();
    }

}
